package com.baide.sanguanheyi.swing;

import com.baide.sanguanheyi.sqlite.Content;
import com.baide.sanguanheyi.sqlite.ContentService;
import org.jdesktop.swingx.JXDatePicker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName SelContent
 * @Description TODO
 * @Author wangbing
 * @Date 2020/5/19 14:07
 **/
@Component
public class SelContent extends JPanel {

    @Autowired
    ContentService service;

    public SelContent() {
        super(new BorderLayout());
        add(top(), BorderLayout.NORTH);
        add(getCenterPanel(), BorderLayout.CENTER);
    }

    private JPanel top() {
        JPanel jPanel = new JPanel();
        TitledBorder titledBorder = BorderFactory.createTitledBorder("查询条件");
        titledBorder.setTitleJustification(TitledBorder.TOP);
        jPanel.setBorder(titledBorder);
        jPanel.setLayout(new BorderLayout());
        jPanel.add(getBox(), BorderLayout.NORTH);
        jPanel.add(getSouth(), BorderLayout.SOUTH);
        return jPanel;
    }

    SelTableModel model;
    JTable jTable;
    List<Content> data = new ArrayList<>();

    public JPanel getCenterPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        model = new SelTableModel(data);
        jTable = new JTable(model);
        jTable.setFont(new Font("Menu.font", Font.PLAIN, 18));
        jTable.getTableHeader().setFont(new Font("Dialog", 0, 18));
        JScrollPane js = new JScrollPane(jTable);
        panel.add(js);
        return panel;
    }

    private JComboBox<String> box;
    JLabel fx, cy, bk;//bg

    private JPanel getSouth() {
        JPanel jPanel = new JPanel();
        JLabel jLabel = new JLabel("放行:");
        jLabel.setFont(new Font("楷体", Font.TYPE1_FONT, 30));
        fx = new JLabel("0");
        fx.setFont(new Font("楷体", Font.TYPE1_FONT, 30));
        fx.setForeground(Color.RED);
        jPanel.add(jLabel);
        jPanel.add(fx);
        JLabel jLabel1 = new JLabel("    查验:  ");
        jLabel1.setFont(new Font("楷体", Font.TYPE1_FONT, 30));
        cy = new JLabel("0");
        cy.setFont(new Font("楷体", Font.TYPE1_FONT, 30));
        cy.setForeground(Color.RED);
        jPanel.add(jLabel1);
        jPanel.add(cy);
        JLabel jLabel2 = new JLabel("    标快:  ");
        jLabel2.setFont(new Font("楷体", Font.TYPE1_FONT, 30));
        bk = new JLabel("0");
        bk.setFont(new Font("楷体", Font.TYPE1_FONT, 30));
        bk.setForeground(Color.RED);
        jPanel.add(jLabel2);
        jPanel.add(bk);
//        JLabel jLabel3 = new JLabel("    其它:  ");
//        jLabel3.setFont(new Font("楷体", Font.TYPE1_FONT, 50));
//        bg = new JLabel("0");
//        bg.setFont(new Font("楷体", Font.TYPE1_FONT, 50));
//        bg.setForeground(Color.RED);
//        jPanel.add(jLabel3);
//        jPanel.add(bg);
        return jPanel;
    }

    private JPanel getBox() {
        JPanel jPanel = new JPanel();
        jPanel.setLayout(new GridLayout(1, 6, 5, 50));
        box = new JComboBox<>();
        box.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        box.addItem("全部");
        box.addItem("放行");
        box.addItem("查验");
//        box.addItem("入库");
//        box.addItem("放行征税");
//        box.addItem("退运");
//        box.addItem("拒识");
//        box.addItem("退单");
        jPanel.add(box);
//        jPanel.add(input());
        jPanel.add(getStartTime());
        jPanel.add(getEndTime());
        JButton button = new JButton("查询");
        button.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String voyageNO = "";
                String boxString = box.getSelectedItem().toString();
                String custOrder = "";
                switch (boxString) {
                    case "全部":
                        custOrder = "";
                        break;
                    case "放行":
                        custOrder = "0";
                        break;
                    case "查验":
                        custOrder = "1";
                        break;
                    case "入库":
                        custOrder = "2";
                        break;
                    case "放行征税":
                        custOrder = "3";
                        break;
                    case "退运":
                        custOrder = "4";
                        break;
                    case "拒识":
                        custOrder = "5";
                        break;
                    case "退单":
                        custOrder = "6";
                        break;
                }
                long startTime = jdp1.getDate().getTime();
                long endTime = jdp2.getDate().getTime() + 24 * 60 * 60 * 1000;
                List<Content> list = service
                        .findByTimeGreaterThanEqualAndTimeLessThanEqualAndVoyageNoContainingAndCustOrderContaining(startTime, endTime, voyageNO, custOrder);
                data.clear();
                data.addAll(list);

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        int i1 = 0, i2 = 0, i3 = 0;
                        for (Content c : list) {
                            if (c.getCustOrder().equals("1")) {//查验
                                i2++;
                            } else if (c.getCustOrder().equals("0")) {//放行
                                i3++;
                                if (c.getBarCode().startsWith("11")) {
                                    i1++;
                                }
                            }
                        }
                        model.fireTableDataChanged();
                        fx.setText(i3 + "");
                        cy.setText(i2 + "");
                        bk.setText(i1 + "");
//                        bg.setText(i4 + "");
                    }
                });
            }
        });
        jPanel.add(button);
        JButton button1 = new JButton("导出");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (data.size() == 0) {
                    JOptionPane.showMessageDialog(null, "暂无导出数据，请先查询相关数据");
                    return;
                }
                export();
            }
        });
        button1.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        jPanel.add(button1);
        JButton button2 = new JButton("导出收寄");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                exportSj();
            }
        });
        button2.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        jPanel.add(button2);
        return jPanel;
    }


    JTextField voyageNoText;

    private JPanel input() {
        JPanel jPanel = new JPanel();
        JPanel lp = new JPanel();
        JLabel label = new JLabel("总号:");
        label.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        JPanel j = new JPanel();
        j.setPreferredSize(new Dimension(50, 30));
        j.add(label);
        lp.add(j);
        JPanel txt = new JPanel();
        voyageNoText = new JTextField();
        voyageNoText.setColumns(10);
        voyageNoText.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        txt.add(voyageNoText);
        jPanel.add(lp);
        jPanel.add(txt);
        return jPanel;
    }

    private JXDatePicker jdp1, jdp2;

    private JPanel getStartTime() {
        JPanel jPanel = new JPanel();
        JLabel label = new JLabel("从");
        label.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        jPanel.add(label);
        jdp1 = new JXDatePicker();
        jdp1.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH, -3);
        jdp1.setDate(now.getTime());
        jPanel.add(jdp1);
        return jPanel;
    }

    private JPanel getEndTime() {
        JPanel jPanel = new JPanel();
        JLabel label = new JLabel("到");
        label.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        jPanel.add(label);
        jdp2 = new JXDatePicker();
        jdp2.setFont(new Font("楷体", Font.TYPE1_FONT, 18));
        jdp2.setDate(new Date());
        jPanel.add(jdp2);
        return jPanel;
    }

    private void export() {
        FileDialog fd = new FileDialog(TableFrame.getjFrame(), "保存表格信息", FileDialog.SAVE);
        fd.setLocation(500, 350);
        fd.setVisible(true);
        String stringfile = fd.getDirectory() + fd.getFile() + ".xls";
        ExcelExporter export = new ExcelExporter();
        export.exportMy(new File(stringfile), data);
    }

    private void exportSj() {
        String voyageNO = "";
        long startTime = jdp1.getDate().getTime();
        long endTime = jdp2.getDate().getTime() + 24 * 60 * 60 * 1000;
        List<Content> c = service.findByTimeGreaterThanEqualAndTimeLessThanEqualAndVoyageNoContainingAndCustOrderContainingAndCtOrderIs(startTime
                , endTime, voyageNO, "0", "C");
        FileDialog fd = new FileDialog(TableFrame.getjFrame(), "保存表格信息", FileDialog.SAVE);
        fd.setLocation(500, 350);
        fd.setVisible(true);
        String stringfile = fd.getDirectory() + fd.getFile() + ".xls";
        ExcelExporter export = new ExcelExporter();
        export.export(new File(stringfile), c);
    }
}
